#include "palette.h"

#include "SDL.h"
#include <stdio.h>

#include "global.h"
#include "data.h"
#include "ui.h"

static SDL_Color original_color[256];


// Load standard palette file and setup SDL palette.
void init_palette()
{
	// load palette file
	FILE* f = fopen(datapath("palette.gpl"), "r");
	if(!f) 
		ui_error(_("File %s not found"), "palette.gpl");

	// skip header
	int c;
	do
	{
		c = fgetc(f);
		if(c == EOF)
			ui_error(_("EOF unexpected in %s"), "palette.gpl");
	}
	while(c != '#');

	// load colors
	int r, g, b, status = 0;
	int n = 0;
	char name[512];
	while(status != EOF)
	{
		status = fscanf(f, "%d %d %d %s", &r, &g, &b, name);
		original_color[n++] = (SDL_Color){ r, g, b };
	}
	fclose(f);

	//set_basic_palette(original_color);
	//setup_palette(screen);
}


void setup_palette(SDL_Surface* sf)
{
	// setup SDL palette
	SDL_SetPalette(sf, SDL_LOGPAL|SDL_PHYSPAL, original_color, 0, 255);
}


void load_default_palette(SDL_Color color[])
{
	int i;
	for(i=0; i<256; i++)
	{
		color[i].r = original_color[i].r;
		color[i].g = original_color[i].g;
		color[i].b = original_color[i].b;
	}
}
